package com.senlan.water.dao.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.senlan.water.common.exception.BusinessException;
import com.senlan.water.common.result.ResultCode;
import com.senlan.water.common.util.DateUtil;
import com.senlan.water.common.util.UserInfoUtil;
import com.senlan.water.dao.domain.Project;
import com.senlan.water.dao.domain.User;
import com.senlan.water.dao.service.ProjectService;
import com.senlan.water.dao.mapper.ProjectMapper;
import com.senlan.water.dto.project.ProjectAddDTO;
import com.senlan.water.dto.project.ProjectEditDTO;
import com.senlan.water.dto.project.ProjectSearchDTO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* @author 10499
* @description 针对表【project(项目)】的数据库操作Service实现
* @createDate 2024-12-11 16:41:00
*/
@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project>
    implements ProjectService{

    @Resource
    private UserInfoUtil userInfoUtil;

    @Override
    public Boolean addNew(ProjectAddDTO addDTO) {
        Project project = new Project();
        BeanUtil.copyProperties(addDTO,project);
        setProjectTimes(addDTO.getPlanTime(), addDTO.getRealityTime(), project);

        //设置用户信息
        User userInfo = userInfoUtil.getUserInfo();
        project.setCreateBy(userInfo.getNickname());
        project.setUpdateBy(userInfo.getNickname());
        return baseMapper.insert(project) > 0;
    }

    @Override
    public Boolean editById(ProjectEditDTO editDTO) {
        Project project = baseMapper.selectById(editDTO.getId());
        if (project == null){
            throw new BusinessException(ResultCode.PROJECT_DATA_NOT_EXITS);
        }
        BeanUtil.copyProperties(editDTO,project);
        setProjectTimes(editDTO.getPlanTime(), editDTO.getRealityTime(), project);

        //设置用户信息
        User userInfo = userInfoUtil.getUserInfo();
        project.setUpdateBy(userInfo.getNickname());
        return baseMapper.insert(project) > 0;
    }

    /**
     * 设置预计时间和实际时间
     * @param planTime
     * @param realityTime
     * @param project
     */
    private void setProjectTimes(List<Date> planTime, List<Date> realityTime, Project project) {
        if (CollectionUtil.isNotEmpty(planTime)) {
            project.setPlanBeginTime(planTime.get(0));
            project.setPlanEndTime(DateUtil.convertDate(planTime.get(1)));
        }
        if (CollectionUtil.isNotEmpty(realityTime)) {
            project.setRealityBeginTime(realityTime.get(0));
            project.setRealityEndTime(DateUtil.convertDate(realityTime.get(1)));
        }
    }


    @Override
    public Boolean deleteById(Long id) {
        Project project = baseMapper.selectById(id);
        if (project == null){
            throw new BusinessException(ResultCode.PROJECT_DATA_NOT_EXITS);
        }
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    public Page<Project> getPage(ProjectSearchDTO searchDTO) {
        LambdaQueryWrapper<Project> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(searchDTO.getTitle()),Project::getTitle,searchDTO.getTitle());
        if (CollectionUtil.isNotEmpty(searchDTO.getPlanBeginTime())){
            queryWrapper.ge(Project::getPlanBeginTime,searchDTO.getPlanBeginTime().get(0));
            queryWrapper.le(Project::getPlanBeginTime, DateUtil.convertDate(searchDTO.getPlanBeginTime().get(1)));
        }
        if (CollectionUtil.isNotEmpty(searchDTO.getCreateTime())){
            queryWrapper.ge(Project::getCreateTime,searchDTO.getCreateTime().get(0));
            queryWrapper.le(Project::getCreateTime, DateUtil.convertDate(searchDTO.getCreateTime().get(1)));
        }
        queryWrapper.eq(searchDTO.getProjectPhase() != null,Project::getProjectPhase,searchDTO.getProjectPhase());
        queryWrapper.eq(searchDTO.getProjectStatus() != null,Project::getProjectStatus,searchDTO.getProjectStatus());

        Page<Project> page = new Page<>();
        page.setCurrent(searchDTO.getPageNum());
        page.setSize(searchDTO.getPageSize());
        return baseMapper.selectPage(page,queryWrapper);
    }
}




